<template>
  <div class="profile-box">
    <div v-if="!user" class="not-login header-bg">
      <img src="@/assets/mobile.png" @click="$router.push('/login')">
      <span>登录/注册</span>
    </div>
    <!-- 已登录---头部 -->
    <div v-else class="login-info header-bg">
      <div class="user-info">
        <div class="user-container">
          <div class="left">
            <van-image class="avatar" round fit="cover" :src="userInfo.photo" />
            <span class="username">{{userInfo.name}}</span>
          </div>
          <!-- 右侧 -->
          <van-button size="mini" round type="default">默认按钮</van-button>
        </div>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <van-grid :column-num="2" class="nav-grid">
      <van-grid-item>
        <i slot="icon" class="link link-shoucang"></i>
        <span slot="text" class="grid-text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="link link-lishi"></i>
        <span slot="text" class="grid-text">历史</span>
      </van-grid-item>
    </van-grid>

    <div class="div_cell">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>

    <van-cell @click="logout" v-if="user" title="退出登录" class="logout-cell" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserProfile } from '@/api/user.js'
export default {
  name: 'profile',
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    logout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认关闭吗'
      })
        .then(() => {
          // on confirm
          this.$store.commit('inituser', null)
        })
        .catch(() => { })
    },
    async loaduser () {
      try {
        const { data: res } = await getUserProfile()
        this.userInfo = res.data
      } catch (err) {
        this.$toast('获取失败')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {},
  created () {
    if (this.user) {
      this.loaduser()
    }
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.profile-box {
  .header-bg {
    width: 750px;
    height: 400px;
    background-image: url(~@/assets/banner.png);
    background-size: 100%;
  }
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  .login-info {
    .user-info {
      padding: 0 32px;
      height: 247px;
      display: flex;
      align-items: flex-end;
      .user-container {
        display: flex;
        width: 100%;
        height: 132px;
        justify-content: space-between;
        align-items: center;
        .left {
          display: flex;
          align-items: center;
          .avatar {
            width: 132px;
            height: 132px;
          }
          .username {
            font-size: 30px;
            margin-left: 22px;
            color: #fff;
          }
        }
      }
    }
    .user-data {
      height: 153px;
      display: flex;
      .data-item {
        flex: 1;
        color: #fff;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 20px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
    }
  }
  .nav-grid {
    .link {
      font-size: 45px;
    }
    .link-shoucang {
      color: #eb5253;
    }

    .link-lishi {
      color: #eb5253;
    }
    .grid-text {
      letter-spacing: 10px;
      text-indent: 10px;
      font-size: 28px;
      margin-top: 16px;
      color: #333;
      text-align: center;
    }
  }
  .div_cell {
    margin: 9px 0;
  }
  .logout-cell {
    text-align: center;
    color: #d86262;
  }
}
</style>
